<?php

declare(strict_types=1);

/**
 * 文章收藏
 * @contact  593809624@qq.com
 * @author   刘传望
 * @date 2025-10-16
 */

namespace App\Repository;

use App\Model\ArticleFavorite;
use Hyperf\Di\Annotation\Inject;

class ArticleFavoriteRepository
{
    #[Inject]
    private ArticleFavorite $articleFavoriteModel;

    /**
     * 检查用户是否已收藏文章
     * @param int $userId
     * @param int $articleId
     * @param int $folderId
     * @return ArticleFavorite|null
     */
    public function findExistingFavorite(int $userId, int $articleId, int $folderId): ?ArticleFavorite
    {
        return $this->articleFavoriteModel->query()
            ->where('user_id', $userId)
            ->where('article_id', $articleId)
            //->where('folder_id', $folderId)
            ->first();
    }

    /**
     * 创建收藏记录
     * @param array $data
     * @return ArticleFavorite
     */
    public function create(array $data): ArticleFavorite
    {
        return $this->articleFavoriteModel->create($data);
    }

    /**
     * 删除收藏记录
     * @param ArticleFavorite $favorite
     * @return bool
     */
    public function delete(ArticleFavorite $favorite): bool
    {
        return $favorite->delete();
    }

    /**
     * 获取文章收藏数量
     * @param int $articleId
     * @return int
     */
    public function getArticleFavoriteCount(int $articleId): int
    {
        return $this->articleFavoriteModel->query()
            ->where('article_id', $articleId)
            ->count();
    }

    /**
     * 获取收藏夹中的文章列表
     * @param int $folderId
     * @param int $size
     * @param int $page
     * @return array
     */
    public function getFolderArticles(int $folderId, int $size = 20, int $page = 1): array
    {
        $query = $this->articleFavoriteModel->query()
            ->where('folder_id', $folderId)
            ->with(['article.user', 'article.tags'])
            ->orderBy('created_at', 'desc');

        $total = $query->count();
        $articles = $query->offset(($page - 1) * $size)
                         ->limit($size)
                         ->get();

        return [
            'articles' => $articles->toArray(),
            'pagination' => [
                'page' => $page,
                'size' => $size,
                'total' => $total,
                'pages' => ceil($total / $size),
            ],
        ];
    }

    /**
     * 获取用户收藏列表（用于导出）
     * @param int $userId
     * @param int|null $folderId
     * @return array
     */
    public function getUserFavorites(int $userId, ?int $folderId = null): array
    {
        $query = $this->articleFavoriteModel->query()
            ->where('user_id', $userId)
            ->with(['article.user', 'folder']);

        if ($folderId) {
            $query->where('folder_id', $folderId);
        }

        return $query->orderBy('created_at', 'desc')->get()->toArray();
    }

    /**
     * 删除收藏夹下的所有收藏
     * @param int $folderId
     * @return int 删除的记录数
     */
    public function deleteByFolderId(int $folderId): int
    {
        return $this->articleFavoriteModel->query()
            ->where('folder_id', $folderId)
            ->delete();
    }

    /**
     * 获取用户对文章的收藏状态
     * @param int $userId
     * @param int $articleId
     * @return array
     */
    public function getUserArticleFavorites(int $userId, int $articleId): array
    {
        return $this->articleFavoriteModel->query()
            ->where('user_id', $userId)
            ->where('article_id', $articleId)
            ->with('folder')
            ->get()
            ->toArray();
    }
}
